﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using MpSoft.Library.DTO;
using MpSoft.Library.BUSS.Contents;

namespace MpSoft.Web.AdminCP.Controls.Content
{
    public partial class VideoEdit : System.Web.UI.UserControl
    {
        #region Var

        private int ItemID = 0;

        #endregion

        #region Method

        private void BindControl()
        {
            SetControl();
            if (ItemID > 0)
            {
                var obj = VideoBUSS.Video_ByID(ItemID);
                if (obj != null)
                {
                    ddlCategory.SelectVlaue = obj.CategoryID;
                    txtTitle.Text = obj.Title;
                    txtDescription.Text = obj.Description;
                    chkIsActive.Checked = obj.IsActive;
                    txtOrderSort.NumberIncremnet = obj.SortOrder;
                    txtImage.Text = obj.Image;
                    txtLinkVideo.Text = obj.LinkVideo;
                    txtWidth.Text = obj.Width;
                    txtHeight.Text = obj.Height;
                }
            }
        }

        private int Save()
        {
            Video obj = null;
            if (ItemID > 0)
                obj = VideoBUSS.Video_ByID(ItemID);
            if (obj == null)
                obj = new Video();
            obj.Title = txtTitle.Text;
            obj.Description = txtDescription.Text;
            obj.IsActive = chkIsActive.Checked;
            obj.SortOrder = txtOrderSort.NumberIncremnet;
            obj.CategoryID = ddlCategory.SelectVlaue;
            obj.Image = txtImage.Text;
            obj.LinkVideo = txtLinkVideo.Text;
            obj.Width = string.IsNullOrEmpty(txtWidth.Text) ? null : txtWidth.Text;
            obj.Height = string.IsNullOrEmpty(txtHeight.Text) ? null : txtHeight.Text;
            if (obj.LinkVideo.ToLower().Contains("youtube"))
                obj.Embed = EmbedYoutube(GetLinkYoutubeID(obj.LinkVideo), obj.Width, obj.Height);
            else
                obj.Embed = EmbedOrther(obj.LinkVideo, obj.Width, obj.Height);
            if (obj.VideoID > 0)
                return VideoBUSS.Video_Update(obj);
            else
                return VideoBUSS.Video_Add(obj);
        }

        private void Message(string message, int type)
        {
            switch (type)
            {
                case 1:
                    ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
                    MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
                    break;
                case 2:
                    ltlMessage.Text = "<p class='message-warning'><i class='glyphicon-warning_sign'></i> " + message + "</p>";
                    break;
            }
        }

        private void SetControl()
        {
            ddlCategory.DdlCategories.CssClass = "form-select";
            ddlCategory.DdlCategories.Attributes.Add("data-rule-required", "true");
            ddlCategory.DdlCategories.Attributes.Add("style", "width:auto;");
            ddlCategory.Module = "video";
        }

        private string EmbedYoutube(string videoid, string width, string height)
        {
            return "<iframe width='" + width + "' height='" + height + "' src='//www.youtube.com/embed/" + videoid + "' frameborder='0' allowfullscreen></iframe>";
        }

        private string EmbedOrther(string linkvideo, string width, string height)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<object width='" + width + "px' height='" + height + "px' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'>");
            sb.Append("<param name='movie' value='/mediaplayer.swf'>");
            sb.Append("<param name='flashvars' value='width=" + width + "&amp;height=" + height + "&amp;file=" + linkvideo + "&amp;image=&amp;displaywidth=" + width + "&amp;autostart=true&amp;repeat=false'>");
            sb.Append("<param name='quality' value='high'>");
            sb.Append("<param name='volume' value='100'>");
            sb.Append("<param name='uiMode' value='full'>");
            sb.Append("<param name='autostart' value='1'>");
            sb.Append("<param name='wmode' value='transparent'>");
            sb.Append("<param name='fullScreen' value='1'>");
            sb.Append("<param name='allowfullscreen' value='true'>");
            sb.Append("<embed width='" + width + "px' height='" + height + "px' autostart='true' volume='-1' allowfullscreen='true' ");
            sb.Append("wmode='transparent' type='application/x-shockwave-flash'");
            sb.Append("flashvars='width=" + width + "&amp;height=" + height + "&amp;file=" + linkvideo + "&amp;image=&amp;displaywidth=" + width + "&amp;autostart=true&amp;repeat=false' src='/mediaplayer.swf'>");
            sb.Append("</object>");
            return sb.ToString();
        }

        private string GetLinkYoutubeID(string linkvideo)
        {
            if (linkvideo.Contains("v"))
            {
                string IdVideo = string.Empty;
                if (linkvideo.Contains("&"))
                {
                    for (int i = linkvideo.IndexOf("=") + 1; i < linkvideo.Length; i++)
                    {
                        if (linkvideo[i].CompareTo('&') == 0) break;
                        IdVideo += linkvideo[i];
                    }
                    return IdVideo;
                }
                for (int i = linkvideo.IndexOf("=") + 1; i < linkvideo.Length; i++)
                    IdVideo += linkvideo[i];
                return IdVideo;
            }
            return "";
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out ItemID);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Message("Cập nhật thành công!", 1);
        }

        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            if (Save() > 0)
                Response.Redirect("?mod=video");
        }

        #endregion

        #region Properties
        #endregion
    }
}